package pf

import (
	"chess_platform/controllers"
	"chess_platform/internal/alisdk"
	. "chess_platform/internal/common"
	"chess_platform/internal/token"
	"fmt"
)

// oss
type OssController struct {
	BaseController
}

func (b *OssController) URLMapping() {
	b.Mapping("UploadFileToOSS", b.UploadFileToOSS)
	b.Mapping("ServerConf", b.ServerConf)
}

//这个controller有点特殊,它要用以h5端使用,也要用以游戏客户端客服反馈
func (b *OssController) Prepare() {
	LogPF.Debug("用户输入:" + string(b.Ctx.Input.RequestBody))
	LogPF.Debug("用户访问url:" + b.Ctx.Input.URL())
	b.Res = new(controllers.Response)
	tokenStr := b.Ctx.Request.PostFormValue("token")
	if tokenStr == "" {
		b.ResponseFail("未带token访问")
		return
	}

	ex, err := token.Parse(tokenStr)
	if err != nil || ex.GetTokenType() != TokenPF {
		LogPF.Error(fmt.Sprintf("token解析错误:%v,或不是此Pf类型的token", err))
		b.Res.Code = ErrUserUnauthorized
		b.Res.Msg = MsgErrUserUnauthorized
		b.Response()
		return
	}
	b.Uid = ex.GetId()
	b.Level = ex.GetLevel()
	status, _ := ex.GetInfo().(float64)
	b.Status = int64(status)
	if b.Status != FirstTopUpSuccess {
		b.Res.Code = ErrUserUnauthorized
		b.Res.Msg = MsgUserNotPass
		b.Response()
		return
	}
	LogPF.Debug(fmt.Sprintf("用户访问(ID:%v,Level:%v,Status:%v)url:%v", b.Uid, b.Level, b.Status, b.Ctx.Input.URL()))

	//cache里不存在说明token已经过期了
	key := fmt.Sprintf("%v%v", KeyPfTokenCache, b.Uid)
	if ex.IsExpire() || !RedisCache.IsExist(key) {
		b.Res.Code = ErrUserUnauthorized
		b.Res.Msg = MsgErrUserExpire
		b.Response()
		return
	}

	//cache里的token与传进来的不一样的话表示此帐号在其它地方登录了
	tokenCache := RedisCache.GetString(key)
	if tokenCache != "" { // token存在缓存里
		if string(tokenCache) != tokenStr {
			b.Res.Code = ErrAlreadyLogin
			b.Res.Msg = MsgErrAlreadyLogin
			b.Response()
			return
		}
	} else { //token不存在缓存里
		b.Res.Code = ErrUserUnauthorized
		b.Res.Msg = MsgErrUserUnauthorized
		b.Response()
		return
	}

}

//要做上传次数限制?, 国华说不用,能用多少图片啊,2018-04-19
// @Title 上传文件图片(可同时三张图片一起上传)
// @Description 上传文件图片(可同时三张图片一起上传)
// @Param   file1        body   string     true        "上传的第一个文件名字"
// @Param   file2        body   string     false       "上传的第二个文件名字"
// @Param   file3        body   string     false       "上传的第三个文件名字"
// @Param   module       body   string     true        "模块名,反馈:Feedback"
// @Success 0 {string} 状态码
// @router /upload [post]
func (b *OssController) UploadFileToOSS() {
	module := b.Ctx.Request.PostFormValue("module")
	savePath := GetPathByModule(module)
	if savePath == "" {
		b.ResponseFail("模块名输入有误")
		return
	}

	f1, h1, err := b.GetFile("file1")
	if err != nil {
		b.ResponseFail(err.Error())
		return
	}
	defer f1.Close()
	ossPath1, err := alisdk.UploadFileToOss(f1, h1.Filename, PlatformRunMode+savePath, b.Uid)
	if err != nil {
		b.ResponseFail(err.Error())
		return
	}
	var ossPath2 string
	f2, h2, err := b.GetFile("file2")
	if err == nil {
		defer f2.Close()
		ossPath2, err = alisdk.UploadFileToOss(f2, h2.Filename, PlatformRunMode+savePath, b.Uid)
		if err != nil {
			b.ResponseFail(err.Error())
			return
		}
	}
	var ossPath3 string
	f3, h3, err := b.GetFile("file3")
	if err == nil {
		defer f3.Close()
		ossPath3, err = alisdk.UploadFileToOss(f3, h3.Filename, PlatformRunMode+savePath, b.Uid)
		if err != nil {
			b.ResponseFail(err.Error())
			return
		}
	}
	b.Res.Data = map[string]interface{}{
		"path1": ossPath1,
		"path2": ossPath2,
		"path3": ossPath3,
	}
	b.ResponseSuccess("上传成功")
}

func GetPathByModule(moduleName string) string {
	switch moduleName {
	case "Feedback":
		return "/feedback"
	}
	return ""
}

// @Title 服务器参数
// @Description 服务器参数
// @Success 0 {object} common.ServerConf 服务器参数
// @router /server-conf [post]
func (b *OssController) ServerConf() {
	serverConf := ServerConf{
		Region:     alisdk.GetOssRegion(),
		Bucket:     alisdk.GetOssBucketName(),
		OssRootUrl: alisdk.GetOssPathUrl(OssDomain),
	}
	b.Res.Data = serverConf
	b.ResponseSuccess()
}
